# Copyright (c) 2025, baijunjie and contributors
# For license information, please see license.txt

import frappe
from frappe.model.document import Document
from frappe.model.docstatus import DocStatus

class Membership(Document):
	def before_submit(self):
		exists = frappe.db.exists(
			"Membership",
			{
				"learner": self.learner,
				"docstatus": DocStatus.submitted(),
				# check if the membership's end date is later than this membership's start date
				"to_data": (">", self.from_date),
			},
		)
		if exists:
			frappe.throw("There is an active membership for this member")
	pass
